<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Worker\Logic;

/**
 * 小哥业务逻辑
 *
 * @author derek <693472824@qq.com>
 */
class WorkerLogic extends BaseLogic {
    
    /**
     * 根据账号获取小哥id
     */
    public static function getIDByAccount($account = ''){
        
        $workerID = '';
        
        if(!empty($account)){
            $workerID = S('account_'.$account);
            
            if(empty($workerID)){
                $workerID = M('Worker')->where(array('account'=>$account))->getField('worker_id');
                S('account_'.$account, $workerID, 604800);
            }
        }
        
        return $workerID;
    }
    
    /**
     * 更新并返回小哥的平均工时
     */
    public function updateHour($workerID = ''){
        
        //每年的平均工时和总单数清零后重新统计，因此 initTime 为年初时间戳
        $initTime = strtotime(date('Y-01-01', time()));
        $hourMap['worker_id'] = $workerID;
        $hourMap['start_time'] = array('EGT', $initTime);
        $avgHour = M('Orders')->where($hourMap)->Avg('service_time');       //单位：分钟
        $avgHour = round($avgHour/60,1) > 99 ? '99' : round($avgHour/60,2); //转换为小时数
        M('Worker')->where(array('worker_id'=>$workerID))->setField('avg_hour', $avgHour);

        return $avgHour;
    }
    
}

?>